package com.just.employment.controller;

import com.just.employment.po.StudentInfo;
import com.just.employment.po.Userlogin;
import com.just.employment.service.EnterpriseWorkService;
import com.just.employment.service.StudentInfoService;
import com.just.employment.utils.FileUploads;
import com.just.employment.utils.Msg;
import com.just.employment.utils.Stringconversion;
import com.just.employment.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@CrossOrigin
@RequestMapping("/student")
public class StudentInfoController {

   @Autowired
    private StudentInfoService studentInfoService;
    @Resource(name="enterpriseWorkServiceImpl")
    private EnterpriseWorkService enterpriseWorkService;
    public Stringconversion scn = new Stringconversion();
    //字符转换工具类
    public FileUploads fileUploads = new FileUploads();
    //文件上传工具类
    public String uid = null;

    //个人信息
    @RequestMapping("/studentinfo")
    @ResponseBody
    public StudentInfo studentinfo(String uid){
        StudentInfo studentInfo = studentInfoService.selectInfo(uid);
        return studentInfo;
    }

    //账户管理跳转
    @RequestMapping("/management")
    public String touxHui(String type,Model model,HttpServletRequest request){
        HttpSession session = request.getSession();
        Userlogin userlogin = (Userlogin) session.getAttribute("logininfo");
        StudentInfo studentInfo = studentInfoService.selectInfo(userlogin.getUid());
        model.addAttribute("user",studentInfo);
        uid = userlogin.getUid();
        if(type.equals("avatar")){
            return "student/avatarmodification";
        }else{
            return "student/updatepassword";
        }
    }

    //修改头像
    @RequestMapping("/savetoux")
    @ResponseBody
    public String savetoux( @RequestParam("file") MultipartFile file,HttpServletRequest request) throws IOException {
        String pathurl = fileUploads.fileUploads(file,request);
        int imgs = studentInfoService.inserttouximg(pathurl,uid);
        if(imgs > 0){
            return "true";
            //跳转到账户管理
        }else {
            return "false";
        }
    }

    //修改密码 --- 待
    @RequestMapping("/savePsw")
    @ResponseBody
    public Msg savePsw(String passwd,String npasswd,String recode,HttpServletRequest request) {
        String checkcode_right = (String) request.getSession().getAttribute("CHECKCODE_SERVER");
        //1.2获取验证码参数值
        request.getSession().removeAttribute("CHECKCODE_SERVER");
        if (recode == null || !recode.equalsIgnoreCase(checkcode_right)) {
            return Msg.success("验证码错误");
        } else {
            //查询旧密码
            HttpSession session = request.getSession();
            Userlogin userlogin = (Userlogin) session.getAttribute("logininfo");
            String npwd = studentInfoService.selectOldPwd(userlogin.getUid());
            if(!npwd.equals(passwd)){
                return Msg.success("旧密码不正确");
            }
            int i = studentInfoService.savePwd(userlogin.getUid(),npasswd);
            if(i > 0){
                return Msg.success("修改密码成功");
            }else {
                return Msg.fail("未知错误");
            }

        }
    }


    //修改个人信息
    @RequestMapping("/updatastuinfo")
    @ResponseBody
    public Result UpdataStuInfo(StudentInfo studentInfo,String type){
        Result result = new Result();
        int i = studentInfoService.updateStudentInfo(studentInfo,type);
        if(i > 0){
            result.setStatus(true);
            result.setMsg("编辑成功");
        }else {
            result.setStatus(false);
            result.setMsg("编辑失败！请稍后再试");
        }
        return result;
    }
}
